#!/usr/bin/perl
# Version 0.2.1

$testcase = "lsa_retrans";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3 );

$success = 1;
$state = 0;
$last1 = 0;
$last2 = 0;
open(DUMPFILE, $dumpfile) or $success = 0;

if($success eq 1) {
	while(<DUMPFILE>) {
		@al = split(/ /, $_);
			
		# look at the time
		@ts = split(/:/, $al[0]);
		$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60;
			
		if($state eq 0) {
			if($secs > 50) {
				$state = $state | 1;
			}
		} else {
			if(/0\.0\.0\.25 > (.*) OSPFv2-ls_upd (.*) rtr 0\.0\.0\.13/) {
				if($secs > 90) {
					# bad bad bad...
					$state = $state | 2;
				} else {
					if($last1 ne 0) {
						# found a retransmission
						$diff = $secs - $last1;
						if(($diff < 4.8)||($diff > 5.2)) { # .2s tolerance
						 $sate = $state | 4;
						}	
					} 
					$last1 = $secs;
				}
				
			}
			if(/0\.0\.0\.21 > (.*) OSPFv2-ls_upd (.*) rtr 0\.0\.0\.13/) {
				if($secs > 90) {
					# bad bad bad...
					$state = $state | 2;
				} else {
					if($last2 ne 0) {
						# found a retransmission
						$diff = $secs - $last2;
						if(($diff < 4.8)||($diff > 5.2)) { # .2s tolerance
						 $sate = $state | 4;
						}	
					} 
					$last2 = $secs;
				}
			}
		}
	}
	
	close(DUMPFILE);
	
	if($state eq 1) {
		$local_verdict[0] = 1;
		print("RUT retransmits packets every RxmtInterval. Test PASSED.\n");
	} else {
		$local_verdict[0] = 2;
		if($state & 2) {
			print("RUT still retransmits after RouterDeadInterval. ");
		}
		if($state & 4) {
			print("Retransission interval not equal to RxmtInterval. ");
		}
		
		print("Test FAILED.\n");
	}
}

if($local_verdict[0] eq 3) {
	print("Cannot evaluate results. Test INCONCLUSIVE.\n");
}


print("Broadcast networks not supported yet. Test INCONCLUSIVE.\n");

$verdict = $local_verdict[0];
if ($local_verdict[1] ne $verdict) {
	if($local_verdict[1] eq 2) {
		$verdict = 2;
	} elsif ($local_verdict[1] eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");